package cn.gcyl.module.biz.controller.admin.useractionlog;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.CommonResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import static cn.gcyl.framework.common.pojo.CommonResult.success;

import cn.gcyl.framework.excel.core.util.ExcelUtils;

import cn.gcyl.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gcyl.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.gcyl.module.biz.controller.admin.useractionlog.vo.*;
import cn.gcyl.module.biz.dal.dataobject.useractionlog.UserActionLogDO;
import cn.gcyl.module.biz.service.useractionlog.UserActionLogService;

@Tag(name = "管理后台 - 用户操作日志")
@RestController
@RequestMapping("/biz/user-action-log")
@Validated
public class UserActionLogController {

    @Resource
    private UserActionLogService userActionLogService;

    @PostMapping("/create")
    @Operation(summary = "创建用户操作日志")
//    @PreAuthorize("@ss.hasPermission('biz:user-action-log:create')")
    public CommonResult<Long> createUserActionLog(@Valid @RequestBody UserActionLogSaveReqVO createReqVO) {
        return success(userActionLogService.createUserActionLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户操作日志")
//    @PreAuthorize("@ss.hasPermission('biz:user-action-log:update')")
    public CommonResult<Boolean> updateUserActionLog(@Valid @RequestBody UserActionLogSaveReqVO updateReqVO) {
        userActionLogService.updateUserActionLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户操作日志")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('biz:user-action-log:delete')")
    public CommonResult<Boolean> deleteUserActionLog(@RequestParam("id") Long id) {
        userActionLogService.deleteUserActionLog(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户操作日志")
//                @PreAuthorize("@ss.hasPermission('biz:user-action-log:delete')")
    public CommonResult<Boolean> deleteUserActionLogList(@RequestParam("ids") List<Long> ids) {
        userActionLogService.deleteUserActionLogListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户操作日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('biz:user-action-log:query')")
    public CommonResult<UserActionLogRespVO> getUserActionLog(@RequestParam("id") Long id) {
        UserActionLogDO userActionLog = userActionLogService.getUserActionLog(id);
        return success(BeanUtils.toBean(userActionLog, UserActionLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户操作日志分页")
//    @PreAuthorize("@ss.hasPermission('biz:user-action-log:query')")
    public CommonResult<PageResult<UserActionLogRespVO>> getUserActionLogPage(@Valid UserActionLogPageReqVO pageReqVO) {
        PageResult<UserActionLogDO> pageResult = userActionLogService.getUserActionLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserActionLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户操作日志 Excel")
//    @PreAuthorize("@ss.hasPermission('biz:user-action-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserActionLogExcel(@Valid UserActionLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserActionLogDO> list = userActionLogService.getUserActionLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户操作日志.xls", "数据", UserActionLogRespVO.class,
                        BeanUtils.toBean(list, UserActionLogRespVO.class));
    }

}